using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using igp.net.DataModel;

namespace igp.net
{
	/// <summary>
	/// Represents the picker window.
	/// </summary>
	public partial class FormPicker : Form
	{

		internal FormPicker()
		{
			InitializeComponent();
		    imageList1.Images.Add(Properties.Resources.chart_line);
		    imageList1.Images.Add(Properties.Resources.chart_curve);
		    imageList1.Images.Add(Properties.Resources.add_function);
		    imageList1.Images.Add(Properties.Resources.tag_blue);
		    imageList1.Images.Add(Properties.Resources.chart_bar);
		}


		private void FormPicker_Load(object sender, EventArgs e)
		{
			comboBox1.SelectedIndex = 0;
			RefreshList();
		} 

        /// <summary>
        /// Refreshes the items list.
        /// </summary>
		public void RefreshList()
		{
            dataGridView1.Rows.Clear();

            foreach( IgpItem item in IgpProject.Singleton.Items)
            {
                System.Drawing.Image im = null;
                
                switch (item.TypeID)
                {
                    case IgpItemType.Function : im = imageList1.Images[1]; break;
                    case IgpItemType.PointSet : im = imageList1.Images[0]; break;
                    case IgpItemType.Label : im = imageList1.Images[3]; break;
                    case IgpItemType.Integral : im = imageList1.Images[4]; break;
                    case IgpItemType.Polynomial : im = imageList1.Images[2]; break;
                }
                                
                dataGridView1.Rows.Add(new object[] {
                    im,
                    item.ItemName,
                    item.IsVisible,
                    item.VisibleOnLegend
                });
             }

            UpdateCaption();
		}

		private void FormPicker_Closing(object sender, CancelEventArgs e)
		{
			e.Cancel = true;
			Hide();
		}

		private void ApplyItemsSelection()
		{
			DataGridView myGrid = dataGridView1;
			
            IEnumerator item = IgpProject.Singleton.Items.GetEnumerator();
			item.Reset();

			for (int a=0; a < myGrid.Rows.Count ; a++)
			{
			    item.MoveNext();
			    ((IgpItem)item.Current).IsSelected = myGrid.SelectedRows.Contains(myGrid.Rows[a]);
			}

		    UpdateCaption();
		}

		private void UpdateCaption()
		{
            Text = string.Format("Item Picker: {0} [{1}]", IgpProject.Singleton.Items.Count, IgpProject.Singleton.GetSelectedCount());
		}

        private void addPointSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IgpItem item = IgpProject.Singleton.AddPointSet();
            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            FormMain.Singleton.PickerWindow.RefreshList();

            IgpProject.Singleton.CurrentPointset = (igpPointSet)item;
            FormMain.Singleton.EditorWindow.SetActivePage(1);
            FormMain.Singleton.EditorWindow.ReadPointSetProperties();

            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
            }
        }

        private void addFunctionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IgpItem item = IgpProject.Singleton.AddFunction();
            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            FormMain.Singleton.PickerWindow.RefreshList();

            IgpProject.Singleton.CurrentFunction = (IgpFunction)item;
            FormMain.Singleton.EditorWindow.SetActivePage(2);
            FormMain.Singleton.EditorWindow.ReadFunctionProperities();

            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
            }
        }

        private void addPolynomialToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IgpItem item = IgpProject.Singleton.AddPolynomial();
            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            FormMain.Singleton.PickerWindow.RefreshList();

            IgpProject.Singleton.CurrentPolynomial = (igpPolynomial)item;
            FormMain.Singleton.EditorWindow.SetActivePage(3);
            FormMain.Singleton.EditorWindow.ReadPolynomialProperities();

            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
            }
        }

        private void addLabelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IgpItem item = IgpProject.Singleton.AddLabel();
            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            FormMain.Singleton.PickerWindow.RefreshList();

            IgpProject.Singleton.CurrentLabel = (IgpLabel)item;
            FormMain.Singleton.EditorWindow.SetActivePage(4);
            FormMain.Singleton.EditorWindow.ReadLabelProperities();

            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
            }
        }

        private void addLabelsForAxesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IgpLabel l = IgpProject.Singleton.AddLabel();
            l.ItemName = "x";
            l.X = IgpProject.Singleton.View.Width - 30;
            l.FixedX = true;
            l.Y = -IgpProject.Singleton.View.GridY / 10;
            l.FixedY = false;
            l.DrawTextOnly = true;

            l = IgpProject.Singleton.AddLabel();
            l.ItemName = "y";
            l.Y = 10;
            l.FixedY = true;
            l.X = IgpProject.Singleton.View.GridX / 10;
            l.FixedX = false;
            l.DrawTextOnly = true;

            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();

            FormMain.Singleton.PickerWindow.RefreshList();
        }

        private void addIntegralToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                IgpItem item = IgpProject.Singleton.AddIntegral();
                FormMain.Singleton.Render();
                FormMain.Singleton.PaintView();
                FormMain.Singleton.PickerWindow.RefreshList();

                IgpProject.Singleton.CurrentIntegral = (igpIntegral)item;
                FormMain.Singleton.EditorWindow.SetActivePage(5);
                FormMain.Singleton.EditorWindow.ReadIntegralProperities();

                if (dataGridView1.Rows.Count > 0)
                {
                    dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
                }
            }
            catch (Exception ex)
            {
                FormMain.Singleton.LogMessage(ex.Message);
            }
        }

        private void inverseVisibilityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (IgpItem item in IgpProject.Singleton.Items)
            {
                item.IsVisible = !item.IsVisible;
            }

            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            FormMain.Singleton.PickerWindow.RefreshList();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var toBeRemoved = new List<IgpItem>();

            foreach (IgpItem item in IgpProject.Singleton.Items)
            {
                if (!item.IsSelected) continue;
                toBeRemoved.Add(item);
            }

            foreach (IgpItem item in toBeRemoved)
            {
                IgpProject.Singleton.Items.Remove(item);
            }

            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            IgpProject.Singleton.VerifyList();
            FormMain.Singleton.EditorWindow.VerifyTabs();
            RefreshList();
        }



        private void deleteInvisibleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var toBeRemoved = new List<IgpItem>();

            foreach (IgpItem item in IgpProject.Singleton.Items)
            {
                if (item.IsVisible) continue;
                toBeRemoved.Add(item);
            }

            foreach (IgpItem item in toBeRemoved)
            {
                IgpProject.Singleton.Items.Remove(item);
            }

            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            IgpProject.Singleton.VerifyList();
            FormMain.Singleton.EditorWindow.VerifyTabs();
            RefreshList();
        }

        private void deleteAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IgpProject.Singleton.Items.Clear();

            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
            IgpProject.Singleton.VerifyList();
            FormMain.Singleton.EditorWindow.VerifyTabs();
            RefreshList();
        }

	    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IgpProject.Singleton.GetCount() == 0 && e.RowIndex < 0) return;
            IgpItem it = IgpProject.Singleton.GetItem(e.RowIndex);

            switch (e.ColumnIndex)
            {
                case 2: ((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value = !(bool)((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value; it.IsVisible = (bool)((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value; break;
                case 3: ((DataGridView)sender).Rows[e.RowIndex].Cells[3].Value = !(bool)((DataGridView)sender).Rows[e.RowIndex].Cells[3].Value; it.VisibleOnLegend = (bool)((DataGridView)sender).Rows[e.RowIndex].Cells[3].Value; break;
            }

            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            EditSelectedItem((DataGridView) sender);
        }

        private static void EditSelectedItem(DataGridView mygrid)
	    {
	        if (IgpProject.Singleton.GetCount() <= 0) return;

	        //Getting clicked item
	        IgpItem item = null;
            
            if (mygrid.CurrentRow != null) item = IgpProject.Singleton.GetItem(mygrid.CurrentRow.Index);

            if (item == null) return;

	        //getting its properities
	        switch (item.TypeID)
	        {
	            case IgpItemType.PointSet: IgpProject.Singleton.CurrentPointset = (igpPointSet)item;
	                FormMain.Singleton.EditorWindow.SetActivePage(1);
	                FormMain.Singleton.EditorWindow.ReadPointSetProperties();
	                break;
	            case IgpItemType.Polynomial: IgpProject.Singleton.CurrentPolynomial = (igpPolynomial)item;
	                FormMain.Singleton.EditorWindow.SetActivePage(3);
	                FormMain.Singleton.EditorWindow.ReadPolynomialProperities();
	                break;
	            case IgpItemType.Label: IgpProject.Singleton.CurrentLabel = (IgpLabel)item;
	                FormMain.Singleton.EditorWindow.SetActivePage(4);
	                FormMain.Singleton.EditorWindow.ReadLabelProperities();
	                break;
	            case IgpItemType.Function: IgpProject.Singleton.CurrentFunction = (IgpFunction)item;
	                FormMain.Singleton.EditorWindow.SetActivePage(2);
	                FormMain.Singleton.EditorWindow.ReadFunctionProperities();
	                break;
	            case IgpItemType.Integral: IgpProject.Singleton.CurrentIntegral = (igpIntegral)item;
	                FormMain.Singleton.EditorWindow.SetActivePage(5);
	                FormMain.Singleton.EditorWindow.ReadIntegralProperities();
	                break;
	        }
	    }

	    private void dataGridView1_Click(object sender, EventArgs e)
        {
            ApplyItemsSelection();
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            int a = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Selected)
                {
                    IgpItem it = IgpProject.Singleton.GetItem(a);
                    System.IO.StringWriter sw = new System.IO.StringWriter();
                    it.SaveData(sw);

                    switch (it.TypeID)
                    {
                        case IgpItemType.PointSet: IgpProject.Singleton.AddPointSet().ReadData(new System.IO.StringReader(sw.ToString()));
                            break;
                        case IgpItemType.Function: IgpProject.Singleton.AddFunction().ReadData(new System.IO.StringReader(sw.ToString()));
                            break;
                        case IgpItemType.Polynomial: IgpProject.Singleton.AddPolynomial().ReadData(new System.IO.StringReader(sw.ToString()));
                            break;
                        case IgpItemType.Label: IgpProject.Singleton.AddLabel().ReadData(new System.IO.StringReader(sw.ToString()));
                            break;
                        case IgpItemType.Integral: IgpProject.Singleton.AddIntegral().ReadData(new System.IO.StringReader(sw.ToString()));
                            break;
                    }
                }

                a++;
            }

            RefreshList();
            FormMain.Singleton.Render();
            FormMain.Singleton.PaintView();
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            EditSelectedItem(dataGridView1);
        }
	}
}
